]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/Tests/Unit Tests/PathEvaluationTest.m
mDNSResponder-1310.40.42.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / Tests / Unit Tests / PathEvaluationTest.m
1 /*
2 * Copyright (c) 2019-2020 Apple Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "unittest_common.h"
18 #import <XCTest/XCTest.h>
19
20 #import <NetworkExtension/NEPolicySession.h>
21
22 @interface PathEvaluationTest : XCTestCase
23 {
24 }
25 @end
26
27 @implementation PathEvaluationTest
28
29 - (void)setUp
30 {
31 mDNSPlatformMemZero(&mDNSStorage, sizeof(mDNS));
32 init_mdns_environment(mDNStrue);
33 }
34
35 - (void)tearDown
36 {
37 }
38
39 - (void)testPathDeny
40 {
41 if(!getenv("DNSSDUTIL_XCTEST")) return; // Don't run without this environment variable
42 DNSQuestion q;
43 mDNSInterfaceID routableIndex;
44
45 mDNSPlatformMemZero(&q, sizeof(DNSQuestion));
46 q.TargetQID.NotAnInteger = 1;
47 q.pid = getpid();
48 q.InterfaceID = if_nametoindex( "pdp_ip0" );
49 fprintf(stdout, "%s %s with cellular index %d named pdp_ip0\n", q.InterfaceID ? "Starting" : "Exiting (no cellular interface)", __FUNCTION__, q.InterfaceID);
50 if (!q.InterfaceID) return;
51
52 routableIndex = IndexForInterfaceByName_ut( "pdp_ip0" );
53 fprintf(stdout, "Testing blocked by (%s)\n", routableIndex ? "policy" : "no route");
54
55 mDNSPlatformGetDNSRoutePolicy(&q);
56 XCTAssertFalse(q.BlockedByPolicy);
57
58 // Now block it
59 NSMutableArray *routeRules = [NSMutableArray array];
60 NEPolicyRouteRule *routeRule = [NEPolicyRouteRule routeRuleWithAction:NEPolicyRouteRuleActionDeny forType:NEPolicyRouteRuleTypeCellular];
61 [routeRules addObject:routeRule];
62 routeRule = [NEPolicyRouteRule routeRuleWithAction:NEPolicyRouteRuleActionDeny forType:NEPolicyRouteRuleTypeWiFi];
63 [routeRules addObject:routeRule];
64 routeRule = [NEPolicyRouteRule routeRuleWithAction:NEPolicyRouteRuleActionDeny forType:NEPolicyRouteRuleTypeWired];
65 [routeRules addObject:routeRule];
66
67 NEPolicyResult *result = [NEPolicyResult routeRules:routeRules];
68 NEPolicy *policy = [[NEPolicy alloc] initWithOrder:1 result:result conditions:@[ [NEPolicyCondition effectivePID:q.pid], [NEPolicyCondition allInterfaces] ]];
69
70 NEPolicySession *policySession = [[NEPolicySession alloc] init];
71 XCTAssertNotNil(policySession, "Check entitlemnts");
72 [policySession addPolicy:policy];
73 [policySession apply];
74
75 mDNSPlatformGetDNSRoutePolicy(&q);
76 // Either if these asserts indicate a regression in mDNSPlatformGetDNSRoutePolicy
77 if (routableIndex) XCTAssertTrue(q.BlockedByPolicy, "blocked by (policy) test failure");
78 else XCTAssertFalse(q.BlockedByPolicy, "blocked by (no route) test failure");
79
80 [policySession removeAllPolicies];
81 [policySession apply];
82 fprintf(stdout, "Completed %s\n", __FUNCTION__);
83 }
84
85 @end